package com.goverse.mancraft.world;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by gaoyu on 2017/4/15.
 */

public class WorldTimer {

    public enum DayMode {
        DAY,
        NIGHT
    }

    public interface OnTimePassListener {
        public void onTimePassed(Time time);
    }

    private OnTimePassListener mOnTimePassListener;

    private ArrayList<OnTimePassListener> mTimePassListeners = new ArrayList<>();

    public void addOnTimePassListener(OnTimePassListener onTimePassListener) {
        if (!mTimePassListeners.contains(onTimePassListener)) mTimePassListeners.add(onTimePassListener);
    }

    public void removeOnTimePassListener(OnTimePassListener onTimePassListener) {
        if (mTimePassListeners.contains(onTimePassListener)) mTimePassListeners.remove(onTimePassListener);
    }

    private void notifyOnTimePassListener() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < mTimePassListeners.size(); i++) {
                    mTimePassListeners.get(i).onTimePassed(mCurrTime);
                }
            }
        });

    }

    public interface OnDayModeChangeListener {
        public void onDayModeChanged(DayMode dayMode);
    }

    private OnDayModeChangeListener mOnDayModeChangeListener;

    public void setOnDayModeChangeListener(OnDayModeChangeListener onDayModeChangeListener) {
        mOnDayModeChangeListener = onDayModeChangeListener;
    }

    private static WorldTimer mInstance;

    public static WorldTimer getInstance() {
        if (null == mInstance) {
            synchronized (WorldTimer.class) {
                if (null == mInstance) {
                    mInstance = new WorldTimer();
                }
            }
        }
        return mInstance;
    }

    private Timer mTimer;

    private TimerTask mTimerTask;

    private Time mCurrTime;

    private WorldTimer() {
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                updateTimer();
            }
        };
    }

    public Time getCurrTime() {
        return mCurrTime;
    }

    public void start(Time time) {
        mCurrTime = time;
        mTimer.schedule(mTimerTask, 0, 1000);
    }

    private void updateTimer() {

        int sec = mCurrTime.getSec();
        int min = mCurrTime.getMin();
        int hour = mCurrTime.getHour();
        if (sec < 10) {
            mCurrTime.setSec(sec + 1);
        } else if (sec == 10 && min < 9) {
            mCurrTime.setSec(1);
            mCurrTime.setMin(min + 1);
        } else if (sec == 10 && min == 9 && hour < 12) {
            mCurrTime.setSec(1);
            mCurrTime.setMin(0);
            mCurrTime.setHour(hour + 1);
        } else if (sec == 10 && min == 9 && hour == 12) {
            mCurrTime.setSec(1);
            mCurrTime.setMin(0);
            mCurrTime.setHour(1);
        }
        if (sec == 1 && min == 0 && hour == 0) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    if (mOnDayModeChangeListener != null) mOnDayModeChangeListener.onDayModeChanged(DayMode.DAY);
                }
            });

        }

        if (sec == 1 && min == 0 && hour == 10) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    if (mOnDayModeChangeListener != null) mOnDayModeChangeListener.onDayModeChanged(DayMode.NIGHT);
                }
            });
        }
        notifyOnTimePassListener();
        Log.d("ManCraft", "Time:----------" + hour + "小时" + min + "分钟" + sec + "秒----------");
    }

}
